package com.ytjj.qmyx.supplychain.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.model.request.InterceptRequest;
import com.ytjj.qmyx.supplychain.common.model.response.InterceptResponse;
import com.ytjj.qmyx.supplychain.mapper.OrdersInterceptMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBanker;
import com.ytjj.qmyx.supplychain.mapper.entity.YxOrdersIntercept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class OrdersInterceptService {
    @Resource
    private OrdersInterceptMapper ordersInterceptMapper;
    @Autowired
    private BankerService bankerService;

    public CommonPage<InterceptResponse> getInterceptOrderList(InterceptRequest interceptRequest) {
        YxBanker userInfo = bankerService.getUserInfo();
        if (userInfo.getLevel() != 0) {
            interceptRequest.setBankerId(userInfo.getId());
        } else {
            interceptRequest.setBankerId(null);
        }
        PageHelper.startPage(interceptRequest.getPageNum(), interceptRequest.getPageSize());
        return CommonPage.restPage(ordersInterceptMapper.getInterceptOrderList(interceptRequest));
    }

    public int addIntercept(YxOrdersIntercept intercept) {
        return ordersInterceptMapper.insert(intercept);
    }

    public int updateStatus(Integer id, Integer status) {
        YxBanker userInfo = bankerService.getUserInfo();
        YxOrdersIntercept yxOrdersIntercept = new YxOrdersIntercept();
        yxOrdersIntercept.setId(id);
        yxOrdersIntercept.setStatus(status);
        yxOrdersIntercept.setLaunchTime(new Date());
        yxOrdersIntercept.setCustomerId(userInfo.getId());
        yxOrdersIntercept.setCustomerName(userInfo.getName());
        return ordersInterceptMapper.updateStatus(yxOrdersIntercept);
    }
}
